﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Input;
using Pavilion.DesignerTool.ViewTools;

namespace Pavilion.DesignerTool
{
    public class LineAdorner : Adorner
    {
        private Point DEFAULT_pointOnCanvas = new Point(50,50);
        public Point StartPoint { get; private set; }
        private Point End { get; set; }

        public LineAdorner(UIElement adornedElement, Point start) : base(adornedElement)
        {
            if (!(adornedElement is Canvas)) throw new ArgumentException("Line adorner may only adorn canvas.");

            StartPoint = start;
            End = GetMousePositionOnCanvas();

            Loaded += (sender, e) => base.CaptureMouse();
           
            MouseMove += (sender, e) =>
                {
                    End = GetMousePositionOnCanvas();
                    base.InvalidateVisual(); //Forces redraw
                };          
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            var brush = new SolidColorBrush(Colors.Black); 
            var pen = new Pen(brush, 1); 
            drawingContext.DrawLine(pen, StartPoint, End);
        }

        private Point GetMousePositionOnCanvas()
        {
            var canvas = this.AdornedElement as Canvas;
        
            if (canvas == null)
                return DEFAULT_pointOnCanvas;

            return Mouse.GetPosition(canvas); 
        }
    }
}
